package javamobile.billsplit;

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javamobile.utils.MySocket;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class PendingBillsActivity extends Activity {
	
	public static final String PREFS_NAME = "MyPrefsFile";
    private Button homeButton; 
    private Button friendsButton; 
    private Button createBillButton; 
    private Button closedBillsButton; 
    private TableLayout pendingTable; 
    
    View.OnClickListener homeHandler = new View.OnClickListener() {
    	public void onClick(View v) {
    		startActivity(new Intent(PendingBillsActivity.this, HomeActivity.class));   
    	}
    };  
    View.OnClickListener friendsHandler = new View.OnClickListener() {
    	public void onClick(View v) {
    		startActivity(new Intent(PendingBillsActivity.this, FriendsActivity.class)); 
    	}
    };  
    View.OnClickListener createBillHandler = new View.OnClickListener() {
    	public void onClick(View v) {
    		startActivity(new Intent(PendingBillsActivity.this, CreateBillActivity.class)); 
    	}
    };  
    View.OnClickListener closedBillsHandler = new View.OnClickListener() {
    	public void onClick(View v) {
    		startActivity(new Intent(PendingBillsActivity.this, ClosedBillsActivity.class)); 
    	}
    };
    /*
    View.OnClickListener pay1Handler = new View.OnClickListener() {
    	@Override
    	public void onClick(View v) {
    		startActivity(new Intent(PendingBillsActivity.this, PayBillActivity.class));   
    	}
    };      
    View.OnClickListener pay2Handler = new View.OnClickListener() {
    	public void onClick(View v) {
    		startActivity(new Intent(PendingBillsActivity.this, PayBillActivity.class)); 
    	}
    }; 
    */    
    @SuppressWarnings("unchecked")
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pending_bills); 
        /*
        paybtn1 = (Button) findViewById(R.id.pay_btn1_PENDING); 
        paybtn2 = (Button) findViewById(R.id.pay_btn2_PENDING); 
        
        paybtn1.setOnClickListener(pay1Handler); 
        paybtn2.setOnClickListener(pay2Handler);   
        */
        // *********** MENU BUTTONS ******************** 
        homeButton = (Button) findViewById(R.id.home_button_PENDING_BILLS); 
        friendsButton = (Button) findViewById(R.id.friends_button_PENDING_BILLS); 
        createBillButton = (Button) findViewById(R.id.create_bill_button_PENDING_BILLS);  
        closedBillsButton = (Button) findViewById(R.id.closed_bills_button_PENDING_BILLS); 
        homeButton.setOnClickListener(homeHandler); 
        friendsButton.setOnClickListener(friendsHandler);       
        createBillButton.setOnClickListener(createBillHandler);   
        closedBillsButton.setOnClickListener(closedBillsHandler); 
        
        
        // **************** LOAD BILLS FROM SERVER *****************
        // Shared preference and username 
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        String username = settings.getString("username", "DEFAULT");

        //Intent toPending = new Intent(CreateBillActivity.this, PendingBillsActivity.class);
    	Map<String, Object> input = new HashMap<String, Object>();

    	// SEND INFO TO SERVER
    	input.put("username", username);
    	input.put("action", "open.do");
    	//List<ExtendedBill> bills = new ArrayList<ExtendedBill>();
 		List<String> names = new ArrayList<String>();
 		List<Double> amounts = new ArrayList<Double>();
 		List<String> dates = new ArrayList<String>();
 		List<Integer> billIDs = new ArrayList<Integer>();
    	// CHECK FOR ERROR IN SERVER STUFF 
 		try {
			Map<String, Object> output = MySocket.runSocket(input);
    	    names = (List<String>)output.get("names");
    	    amounts = (List<Double>)output.get("amounts");
    	    dates = (List<String>)output.get("dates");
    	    billIDs = (List<Integer>)output.get("billIDs");
			List<String> errors = (List<String>) output.get("errors");
    	    
    	    if (errors.size()!=0) {
    	    	Intent toError = new Intent(PendingBillsActivity.this, ErrorActivity.class);
    	    	toError.putStringArrayListExtra("errorlist", new ArrayList<String>(errors));
    	    	startActivity(toError);
    	    	finish();
    	    }
    	    
		} catch (UnknownHostException e) {
			System.out.println("UnknownHostException");
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println("IOException");
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			System.out.println("ClassNotFoundException");
			e.printStackTrace();
		}
 		
 		
 		// ***** LOAD BILLS ****
 		for(int i=0; i<names.size(); i++){
 			/*ExtendedBill itemBill = bills.get(i);  
 			String billName = itemBill.getCreaterName(); 
 			String billDate = itemBill.getDate(); 
 			double billAmount = itemBill.getAmount(); 
 			*/ 		
 			String billName = names.get(i); 
 			double billAmount = amounts.get(i);
 			String billDate = dates.get(i); 
 			int billID = billIDs.get(i); 
 			
    		pendingTable = (TableLayout) findViewById(R.id.bill_table_PENDING);
            TextView name= new TextView(PendingBillsActivity.this);
            name.setText(billName+"  ");
            TextView amount = new TextView(PendingBillsActivity.this);
            amount.setText(String.valueOf(billAmount)); 
            TextView date = new TextView(PendingBillsActivity.this); 
            date.setText(billDate); 
            Button payView = new Button(PendingBillsActivity.this); 
            payView.setText("View/Pay");
            
            // Add on clicklistener for each view/pay button
            payView.setId(billID); 
            payView.setOnClickListener(
    	    		new View.OnClickListener() {
    	    	    	public void onClick(View v) {
    	    	    		int billID = v.getId(); 
    	    	    		Intent toView = new Intent(PendingBillsActivity.this, PayBillActivity.class);
    	    	        	toView.putExtra("billID", billID); 
    	    	        	startActivity(toView); 
    	    	    	}      	    
    	    		});    
            
            
            TableRow tr = new TableRow(PendingBillsActivity.this);
            tr.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));
            tr.addView(name);
            tr.addView(amount);
            tr.addView(date);
            tr.addView(payView); 
            pendingTable.addView(tr);
    		
 			
 		}
    }
    
    
    
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) { 
        getMenuInflater().inflate(R.menu.pending_bills, menu); 
        return true; 
    }
    
   /* @Override
    public void onOptionsItemSelected() {
    
    }*/
    

}
